﻿(function() {
    $(function() {

        var _projectService = abp.services.app.project;
        var _$modal = $('#ProjectCreateModal');
        var _$form = _$modal.find('form');


        $('#RefreshButton').click(function () {
            refreshUserList();
        });

        $('.delete-project').click(function () {
            
            var projectId = $(this).attr("data-project-id");
            var projectName = $(this).attr('data-project-name');

            deleteProject(projectId, projectName);
        });

        $('.edit-project').click(function (e) {
            var projectId = $(this).attr("data-project-id");

            e.preventDefault();
            $.ajax({
                url: abp.appPath + 'Projects/EditProjectModal?projectId=' + projectId,
                type: 'POST',
                contentType: 'application/html',
                success: function (content) {
                    $('#ProjectEditModal div.modal-content').html(content);
                },
                error: function (e) { }
            });
        });

        _$form.find('button[type="submit"]').click(function (e) {
            e.preventDefault();
            if (!_$form.valid()) {
                return;
            }

            var project = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js

            abp.ui.setBusy(_$modal);
            _projectService.createProject(project).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see new user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });

        _$modal.on('shown.bs.modal', function () {
            _$modal.find('input:not([type=hidden]):first').focus();
        });

        //初始化分页
        $.paginationInit($('.pagination'), '', 7);

        function refreshUserList() {
            location.reload(true); //reload page to see new user!
        }

        function deleteProject(projectId, projectName) {
            abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DevM'), projectName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _projectService.deleteProject({
                            id: projectId
                        }).done(function () {
                            refreshUserList();
                        });
                    }
                }
            );
        }

        //项目动态


        var _$dynamicInfoModal = $('#DynamicInfoCreateModal');
        var _$dynamicInfoform = _$dynamicInfoModal.find('form');

        $('.add-dynamicinfo').on('click', function () {
            var projectId = $(this).attr('data-project-id');
            _$dynamicInfoform.find('input[name="projectId"]').val(projectId);
        });


        _$dynamicInfoform.find('button[type="submit"]').click(function (e) {
            e.preventDefault();
            if (!_$dynamicInfoform.valid()) {
                return;
            }

            var dynamicInfo = _$dynamicInfoform.serializeFormToObject(); //serializeFormToObject is defined in main.js

            //上传文件
            var resourceIds = resourceUpload();
            dynamicInfo.ResourceIds = resourceIds;

            var _dynamicInfomationService = abp.services.app.dynamicInfomation;

            abp.ui.setBusy(_$dynamicInfoModal);
            _dynamicInfomationService.addDynamicInfomation(dynamicInfo).done(function () {
                _$dynamicInfoModal.modal('hide');
                location.reload(true); //reload page to see new user!
            }).always(function () {
                abp.ui.clearBusy(_$dynamicInfoModal);
            });
        });


        //资源操作
        _$dynamicInfoform.find('.add-resource').click(function () {
            
            var template = _$dynamicInfoform.find('.add-resource-template').html();
            var $container = _$dynamicInfoform.find('.add-resource-container');
            var $item = $(template);
            $item.find('.btn-select-resource').on('click', function () {
                $item.find('input[type="file"]').on('change', function () {
                    $item.find('input[type="text"]').val($(this).val());
                }).click();
            });

            $item.find('.btn-remove-resource').on('click', function () {
                var _$item = $item;
                _$item.remove();
            });
            $container.append($item);
        });

        function resourceUpload() {
            var $inputs = _$dynamicInfoform.find('.add-resource-container input[type="file"]');
            var formData = new FormData();
            $inputs.each(function () {
                formData.append('resource-' + new Date().getTime(), this.files[0]);
            });
            var ids = [];
            $.ajax({
                type: 'post'
                , url: abp.appPath + 'api/services/app/Resources/Post'
                , cache: false
                , dataType: 'JSON'
                , processData: false
                , contentType: false
                , data: formData
                , async: false
            }).done(function (res) {
                console.log(res);
                $.each(res.result.resources, function () {
                    ids.push(this.id);
                });
            });
            return ids;
        }

        //异步载入动态
        $('.dynamicinfo-container').each(function () {
            var projectId = $(this).attr('data-project-id');
            var $othis = $(this);
            var $loading = $othis;
            //abp.ui.setBusy($loading);
            var _dynamicInfomationService = abp.services.app.dynamicInfomation;

            _dynamicInfomationService.getDynamicInfomations({ projectId: projectId, skipCount: 0, maxResultCount: 3 }).done(function (res) {
                var $_othis = $othis;
                if (res.totalCount > 0) {
                    var template = $('#dynamicinfo-templte').html();
                    var $ul = $(template);
                    var liTemplate = $ul.html();
                    $ul.html('');

                    $.each(res.items, function () {
                        var $li = $(liTemplate);
                        $li.find('a').attr('href', abp.appPath + 'DynamicInfomations/Index?informationId=' + this.id + '&projectId=' + projectId);
                        $li.attr('data-dynamicinfo-id', this.id);
                        this.information = this.information || "";
                        var information = this.information.length > 50 ? this.information.substr(0, 50) : this.information;//最多截取50字
                        $li.find('.dynamicinfo-information').append(information);
                        $li.find('.dynamicinfo-creationtime').text(this.creationTime);
                        if (this.totalResources == 0) {
                            $li.find('.dynamicinfo-totalresource').hide()
                        }
                        $ul.append($li);
                    });

                    if (res.totalCount > 3) {
                        var $li = $(liTemplate);
                        $li.find('a').attr('href', abp.appPath + 'DynamicInfomations/Index?projectId=' + projectId);
                        $li.find('.dynamicinfo-information').text(abp.localization.localize('More', 'DevM') + '>>');
                        $li.find('.dynamicinfo-totalresource').hide()
                        $ul.append($li);
                    }
                    $_othis.append($ul);
                }
                abp.ui.clearBusy($loading);
            });
        });

    });
})();
